package com.hpxx.ylzswl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.base.BaseEvent;
import com.hpxx.ylzswl.event.BDnumEvent;
import com.hpxx.ylzswl.event.MoneyEvent;
import com.hpxx.ylzswl.event.OrderIdEvent;
import com.hpxx.ylzswl.event.OrderListEvent;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Request;

import static com.activeandroid.Cache.getContext;

/**
 * Created by Administrator on 2017/11/14.
 */

public class EditPatientActivity extends BaseActivity implements View.OnClickListener, ResultCallBack {
    private Button my_com_updata;
    private ImageView order_wh;
    private TextView tv_name;
    private String name;
    private String bednum;
    private String orderId;
    private TextView tv_bednum;
    private EditText ed_money, ed_bianhao, ed_phone;
    private String bloodnum;
    private String money;
    private String phone;
    private ImageView tv_qr;
    private String bdnum;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xinxi);
    }

    //提交患者信息接口
    private void itemPost(String orderId, String bloodNum, String patientTel, String orderAmount) {
        OKHttpUtils.postAsync(this
                , HttpAddress.HZ_INFO_URL
                , new RequestParams(this).getHzInfoParams(orderId, bloodNum, patientTel, orderAmount), this
                , false, 1);
    }
    //检查血样编号
    private void BloodPost(String bloodNum) {
        OKHttpUtils.postAsync(this
                , HttpAddress.CHECK_BNUM
                , new RequestParams(this).getBloodParams(bloodNum), this
                , false, 2);
    }

    @Override
    public void initView() {
        back();
        setTitle("录入患者信息");
        name = getIntent().getStringExtra("patientName");
        bednum = getIntent().getStringExtra("bedNum");
        orderId = getIntent().getStringExtra("ORDER_ID");
        ed_money = (EditText) findViewById(R.id.ed_money);
        ed_bianhao = (EditText) findViewById(R.id.ed_bianhao);
        ed_phone = (EditText) findViewById(R.id.ed_phone);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_bednum = (TextView) findViewById(R.id.tv_bednum);
        my_com_updata = (Button) findViewById(R.id.my_com_updata);
        order_wh = (ImageView) findViewById(R.id.order_wh);
        tv_qr = (ImageView) findViewById(R.id.tv_qr);
        tv_qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(EditPatientActivity.this, QRCodeActivity.class));
            }
        });
        tv_name.setText(name);
        tv_bednum.setText("病床号：" + bednum);
        ed_money.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        ed_money.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        ed_money.setText(s);
                        ed_money.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    ed_money.setText(s);
                    ed_money.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        ed_money.setText(s.subSequence(0, 1));
                        ed_money.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });
        my_com_updata.setOnClickListener(this);
        order_wh.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_com_updata:
                 money = ed_money.getText().toString().trim();
                 bloodnum = ed_bianhao.getText().toString().trim();
                 phone = ed_phone.getText().toString().trim();
                if (TextUtils.isEmpty(bloodnum)) {
                    ToastUtil.showToast(getApplicationContext(), "请输入血样编号");
                    return;
                }
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast(this, "请输入患者电话");
                    return;
                }

                if (!phone.matches(Constants.PHONE_ZZ)) {
                    ToastUtil.showToast(this, "手机号格式不正确");
                    return;
                }
                if ("0".equals(money) || TextUtils.isEmpty(money)||"0.0".equals(money)||"0.00".equals(money) ||"0.".equals(money)) {
                    ToastUtil.showToast(getApplicationContext(), "请输入大于0的数字");
                    return;
                }
                BloodPost(bloodnum);
                break;
            case R.id.order_wh:
                startActivity(new Intent(this, ChooseActivity.class));
                break;
        }
    }

    @Override
    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
        super.onEventMainThread(event);
        if (event instanceof MoneyEvent) {
            ed_money.setText(((MoneyEvent) event).getMoney());
        }
        if (event instanceof BDnumEvent) {
            ed_bianhao.setText(((BDnumEvent) event).getMoney());
        }
    }

    @Override
    public void onSuccess(String response, int flag) {
        int status = JsonUtil.getInt(response, "status", 0);
        try {
            switch (flag) {
                case 1:
                    DetailDetail(response);
                    break;
                case 2:
                    BloodDetail(response);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            GeneralUtil.tryShow(getContext(), e);
        }
    }

    private void BloodDetail(String response) {
        int status = JsonUtil.getInt(response, "status", 0);
        String msg = JsonUtil.getString(response,"msg","");
        if (status == 1){
            itemPost(orderId, bloodnum, phone, money);
        }else {
            ToastUtil.showToast(getApplicationContext(),msg);
        }
    }

    private void DetailDetail(String response) {
        int status = JsonUtil.getInt(response, "status", 0);
        String msg = JsonUtil.getString(response,"msg","");
        if (status == 1){
            EventBus.getDefault().post(new OrderIdEvent(orderId));
            EventBus.getDefault().post(new OrderListEvent());
            finish();
        }else {
            ToastUtil.showToast(getApplicationContext(),msg);
        }
    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {

    }
}
